//路由跳转的权限校验
const express = require('express');
const route = express.Router();
const {
    jwtDecode
} = require('../utils/token')
const adminModel = require('../db/model/adminModel')
//引入权限中间件
const isLoginMiddle = require('../middleware/isLoginMiddle');
//主要是为了书写一个接口,使用我们封装好的权限验证中间件
route.get("/logout", isLoginMiddle, async (req, res) => {
    const {
        token
    } = req.headers
    //解码token 得到用户名
    const {
        adminName
    } = jwtDecode(token, "xyue")
    console.log(jwtDecode(token, "xyue"));
    try {
        //为什么这里要用await，不加await就直接往下执行，代码就执行完了
        await adminModel.updateOne({
            adminName
        }, {
            $set: {
                status: 0
            }
        })
        res.send({
            code: 10000,
            msg: "退出登录成功",
            data: null
        })
        //检查登录状态码
        const findResult = await adminModel.find({
            adminName
        })
        console.log(findResult);
    } catch (error) {
        console.log("退出登录出现异常" + e.message);
        res.send({
            code: 10001,
            msg: "退出登录出现异常" + e.message,
            data: null
        })

    }
})
module.exports = route